package com.schillerchina.baselibrary.utils;

import android.util.Log;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangzhiguo
 * @date 2019-08-05 09:39
 * @Description: 日志输类
 */

public class LogUtil {

    private enum LogType {
        d, i, e
    }

    //类名
    private static String className;
    //方法名
    private static String methodName;
    //行数
    private static int lineNumber;

    private static String PROJECT_NAME;
//    private static Boolean isDebug = BuildConfig.DEBUG;
    private static final File LOG_DIR = new File(SDCardUtils.getAppLogPath());

    private static FileLogger fileLogger = new FileLogger(LOG_DIR);


    public static void longLog(String tag, String msg) {  //信息太长,分段打印
        //因为String的length是字符数量不是字节数量所以为了防止中文字符过多，
        //  把4*1024的MAX字节打印长度改为2001字符数
        int max_str_length = 2001 - tag.length();
        //大于4000时
        while (msg.length() > max_str_length) {
            Log.i(tag, msg.substring(0, max_str_length));
            msg = msg.substring(max_str_length);
        }
        //剩余部分
        Log.i(tag, msg);
    }

    /**
     * 初始化设置运行日志路径
     *
     * @param path 运行日志路径
     * @return 设置成功返回true，or false
     */
    public static boolean setInitLogPath(String path) {
        if (PROJECT_NAME == null) {
            PROJECT_NAME = path;
            fileLogger = new FileLogger(new File(PROJECT_NAME));
            return true;
        }
        return false;
    }

    private static void getMethodNames(StackTraceElement[] sElements) {
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
    }

    private static String createLog(String log) {

        StringBuilder builder = new StringBuilder();
        builder.append(methodName).append("(").append(className).append(":").append(lineNumber).append(") => ");
        builder.append(log);
        return builder.toString();

    }

    public static void e(String msg) {
        getMethodNames(new Throwable().getStackTrace());
        log(className, splitLongMsg(msg), LogType.e, false);
    }

    public static void e(String msg, boolean logToFile) {
        getMethodNames(new Throwable().getStackTrace());
        log(className, splitLongMsg(msg), LogType.e, logToFile);
    }

    public static void e(Throwable e) {
        getMethodNames(new Throwable().getStackTrace());
        log(className, "", LogType.e, e, true);
    }

    public static void e(String msg, Throwable e) {
        getMethodNames(new Throwable().getStackTrace());
        log(className, msg, LogType.e, e, true);
    }

    /**
     * 记录日志，ERROR message and exception
     *
     * @param msg       错误信息
     * @param e         exception
     * @param logToFile 是否记录到本地文件
     */
    public static void e(String msg, Throwable e, boolean logToFile) {
        getMethodNames(new Throwable().getStackTrace());
        log(className, msg, LogType.e, e, logToFile);
    }


    /**
     * 超长字符串分割
     *
     * @param msg
     * @return
     */
    private static List<String> splitLongMsg(String msg) {
        int maxStrLen = 2001 - className.length();
        List<String> msgList = new ArrayList<>();
        while (msg.length() > maxStrLen) {
            msgList.add(createLog(msg.substring(maxStrLen)));
            msg = msg.substring(maxStrLen);
        }
        msgList.add(createLog(msg));
        return msgList;
    }

    public static void i(String msg) {
        getMethodNames(new Throwable().getStackTrace());
        log(className, splitLongMsg(msg), LogType.i, false);
    }

    public static void i(String msg, boolean logToFile) {
        getMethodNames(new Throwable().getStackTrace());
        log(className, splitLongMsg(msg), LogType.i, logToFile);
    }


    public static void i(String tag, String msg) {
        getMethodNames(new Throwable().getStackTrace());
        log(tag, splitLongMsg(msg), LogType.i, false);
    }

    public static void d(String tag) {
        getMethodNames(new Throwable().getStackTrace());
        log(className, splitLongMsg(tag), LogType.d, false);
    }

    public static void d(String msg, boolean logToFile) {
        getMethodNames(new Throwable().getStackTrace());
        log(className, splitLongMsg(msg), LogType.d, logToFile);
    }

    private static void log(String tag, List<String> msgList, LogType type, boolean logToFile) {
        for (String m : msgList) {
            log(tag, m, type, null, logToFile);
        }
    }

    private static void log(String tag, String msg, LogType type, Throwable e, boolean logToFile) {
//        if (!isDebug) {
//            return;
//        }
        msg = msg != null ? msg : "";
        String err = e != null ? e.getMessage() != null ? e.getMessage() : "" : "";
        switch (type) {
            case i:
                Log.i(tag, msg);
                break;
            case d:
                Log.d(tag, msg);
                break;
            case e:
                if (e == null) {
                    Log.e(tag, msg);
                } else {
                    Log.e(tag, msg, e);
                }
                break;
            default:
                break;

        }
        if (logToFile) {
//            String.valueOf(BuildConfig.BUILD_TYPE) +
                    fileLogger.log("-" + "-" + tag + ": " + msg + err);
        }
    }

}
